<?php
namespace StarCloud\Library;


use Framework\DiServices\HttpClient;

class Lbs
{
    protected $key = 'XNMBZ-CWLL2-IWIUI-C6P7X-QUCS6-3ZBXR';
    protected $httpClient;

    public function __construct()
    {
        $this->httpClient = new HttpClient(8, 5);
    }

    /**
     * 根据起点坐标和终点坐标测距离[http://lbs.qq.com/webservice_v1/guide-distance.html]
     * @param  [array]   $from [起点坐标(经纬度),例如:array(118.012951,36.810024)]
     * @param  [array]   $to [终点坐标(经纬度)]
     * @return [string]  距离数值
     */
    public function get_distance($from,$to){
        $curl = 'http://apis.map.qq.com/ws/distance/v1/matrix?mode=driving&from='.$from.'&to='.$to.'&key='.$this->key;
        $response = $this->httpClient->sendGetRequest($curl, []);
        $content =  $response->getBody()->getContents();
        $result = json_decode($content,true);
        return $result['result']['rows'];
    }
}